package com.taotao.netty.protobuf.client;

import com.taotao.netty.protobuf.MyMutiDataInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

/**
 * @Description:
 * @Author: TaoJF
 * @Date: Created in 2018/6/30 下午9:13
 */
public class ProtobufClientHandler extends SimpleChannelInboundHandler<MyMutiDataInfo.MyMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyMutiDataInfo.MyMessage msg) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        int randInt = new Random().nextInt(3);
        MyMutiDataInfo.MyMessage myMessage;
        if (0 == randInt) {
            myMessage = MyMutiDataInfo.MyMessage.newBuilder().
                    setDataType(MyMutiDataInfo.MyMessage.DataType.PersonType).
                    setPerson(MyMutiDataInfo.Person.newBuilder().setName("fengxing").setAge(28).setAddress("hangzhou").build()).build();
        } else if (1 == randInt) {
            myMessage = MyMutiDataInfo.MyMessage.newBuilder().
                    setDataType(MyMutiDataInfo.MyMessage.DataType.DogType).
                    setDog(MyMutiDataInfo.Dog.newBuilder().setName("doudou").setAge(8).build()).build();
        } else {
            myMessage = MyMutiDataInfo.MyMessage.newBuilder().
                    setDataType(MyMutiDataInfo.MyMessage.DataType.CatType).
                    setCat(MyMutiDataInfo.Cat.newBuilder().setName("mimi").setColor("yellow").build()).build();
        }
        ctx.channel().writeAndFlush(myMessage);
    }
}
